package com.lyz.core.impl;





import org.apache.log4j.Logger;

import com.lyz.config.ConfigFactory;
import com.lyz.config.IConfig;
import com.lyz.monitor.impl.RealTimeMonitor;
import com.lyz.monitor.impl.ThreadMonitor;
import com.lyz.monitor.impl.ThreadMonitorData;
import com.lyz.util.PropertiesUtil;
/**
 * 程序入口
 * 日志同步客户端，负责日志文件的实时同步传输到日志服务器
 * @author daojian
 *
 */  
public class MainHandler {
	
	private final static Logger logger = Logger.getLogger(MainHandler.class);

	private IConfig config = null;

	public MainHandler(){
		config = ConfigFactory.getConfig();
	}
	
		
	
	public void start()throws Exception{
		int count = 0;
		String[] tags = config.getConfigTags();
		for(String tag : tags){
			
			if(tag==null || "".equals(tag.trim())){
				continue;
			}
			TransferService service = new TransferService(tag);
			service.start();			
			/*加入监控线程列表*/
			ThreadMonitor.getInstance().addMonitorData(new ThreadMonitorData(tag, service));
			count++;
		}
		
		logger.info(count+" channels are starting...");
		
		logger.info("start real time monitor...");
		RealTimeMonitor.getInstance().start();
		ThreadMonitor.getInstance().start();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		logger.info("init configure file...");
		try {
			if(args==null || args.length==0){
				ConfigFactory.getConfig();
			}else{
				ConfigFactory.getConfig(args[0]);
			}
		} catch (Exception e1) {
			logger.error("can not find loggerConfig.propterties! please input param:loggerConfig.propterties path");
			return;
		}
				
		new Thread(){
			
			public void run(){
				MainHandler handler = new MainHandler();
				try {
					handler.start();
				} catch (Exception e) {
					logger.error("program is down!",e);
				}
			}
		}.start();
	}

}
